//
//  DSCallViewController.m
//  DS爱信
//
//  Created by 秦建英 on 14-6-4.
//  Copyright (c) 2014年 秦建英. All rights reserved.
//

#import "DSCallViewController.h"
#import "DingSh.h"
#import "DSHisViewController.h"
@interface DSCallViewController ()

@end

@implementation DSCallViewController
{
    UIButton *_button1;
    UILabel *_label;
    NSMutableArray *_tellArray;
    NSDate* _dat;
    NSString *_date;
    NSUserDefaults *users;
    NSMutableArray *_listItems;
    NSString*timeString;
    NSMutableArray*demoMutArry;
    // NSUserDefaults *myUserDefaults;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // myUserDefaults = [NSUserDefaults standardUserDefaults];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title =@"联系人详情";
    UIBarButtonItem *bbi =[[UIBarButtonItem alloc]initWithTitle:@"<<返回" style:UIBarButtonItemStyleDone target:self action:@selector(bbiClick)];
    self.navigationItem.leftBarButtonItem =bbi;
    self.view.backgroundColor =[UIColor blackColor];
    [self infon];
    
}
-(void)bbiClick
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
-(void)getCurrentTimeOfLabel
{
    
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"HH:mm"];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    NSLog(@"%@",locationString);
    
    //    _timelab.text=locationString;
    //    _timelab.font=[UIFont systemFontOfSize:12];
    NSDate* now = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;
    NSDateComponents *dd = [cal components:unitFlags fromDate:now];
    // int y = [dd year];
    int m = [dd month];
    int d = [dd day];
    
    NSString *day;
    day=[NSString stringWithFormat:@"%d",d];
    if (d<10) {
        day=[NSString stringWithFormat:@"0%d",d];
    }
    
    
    
    timeString=[NSString stringWithFormat:@"%d-%@  %@",m,day,locationString];
    
    
}

-(void)htp:(UIButton*)sender
{
    NSUserDefaults *myUserDefaults = [NSUserDefaults standardUserDefaults];
    UIButton*btn=(UIButton*)sender;
    [self getCurrentTimeOfLabel];
    NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
    NSInteger number=1;
    if([myUserDefaults objectForKey:@"tellDemol"])
    {
        // 如果有直接拿出来继续存(从UserDefaults中取出的任何对象都是不可变的)
        _tellArray = [[NSMutableArray alloc] initWithArray:[myUserDefaults objectForKey:@"tellDemol"]];
        
        demoMutArry=[NSMutableArray arrayWithCapacity:_tellArray.count];
        
        for (int i=0; i<_tellArray.count; i++)
        {
            demoMutArry[i]=[_tellArray[i] objectForKey:@"Demol"];
        }
        
        if ([demoMutArry containsObject:[self SwitchNsstring:btn.currentTitle]])
            
        {
            NSInteger index=  [demoMutArry indexOfObject:[self SwitchNsstring:btn.currentTitle]];
            
            NSInteger number1=[[_tellArray[index] objectForKey:@"Number"]integerValue];
            NSLog(@"ss%ld",(long)number1);
            number1++;
            
            NSMutableDictionary*  dict1=[NSMutableDictionary dictionaryWithDictionary:_tellArray[index]];
            [dict1 setObject:timeString forKey:@"DemolTime"];
            [dict1 setObject:[NSString stringWithFormat:@"%ld",(long)number1] forKey:@"Number"];
            [_tellArray removeObjectAtIndex:index];
            
            [_tellArray insertObject:dict1 atIndex:0];
        }
        else
        {
            [dict setObject:[self SwitchNsstring:btn.currentTitle] forKey:@"Demol"];
            [dict setObject: _label.text forKey:@"Demolname"];
            [dict setObject:timeString forKey:@"DemolTime"];
            [dict setObject:[NSString stringWithFormat:@"%ld",(long)number] forKey:@"Number"];
        }
        
        if ([dict count]==0) {
            
            
        }
        else
        {
            [_tellArray insertObject:dict atIndex:0];
        }
        
        [myUserDefaults setObject:_tellArray forKey:@"tellDemol"];
        
    }
    else
    {
        // 1.如果没有则创建一个新的用户数组
        _tellArray= [[NSMutableArray alloc] initWithCapacity:60];
        
        [dict setObject:[self SwitchNsstring:btn.currentTitle] forKey:@"Demol"];
        [dict setObject:_label.text forKey:@"Demolname"];
        [dict setObject:timeString forKey:@"DemolTime"];
        [dict setObject:[NSString stringWithFormat:@"%ld",(long)number] forKey:@"Number"];
        [_tellArray insertObject:dict atIndex:0];
        NSLog(@"rrrrr%@",_tellArray);
        [myUserDefaults setObject:_tellArray forKey:@"tellDemol"];
    }
    [myUserDefaults synchronize];
}
-(NSString*)SwitchNsstring:(NSString*)string
{
    unichar temp;
    NSString *name=@"";
    
    for (int i=0; i<[string length]; i++) {
        temp=[string characterAtIndex:i];
        NSLog(@"aaa%hu",temp);
        if (temp>='0'&&temp<='9') {
            name=[NSString stringWithFormat:@"%@%@",name,[NSString stringWithFormat:@"%C",temp]];
        }
    }
    return name;
    
}


-(void)infon
{
    _listItems = (NSMutableArray *)[_array componentsSeparatedByString:@"   "];
    for(int i=0;i<_listItems.count;i++)
    {
        NSString *sdfre=[NSString stringWithFormat:@"%@",[_listItems objectAtIndex:i]];
        NSLog(@"qq%@",sdfre);
        if([sdfre isEqualToString:@""])
        {
            [_listItems removeObject:sdfre];
        }
    }
    _label =[[UILabel alloc]initWithFrame:CGRectMake(135, 110, 180, 40)];
    _label.text =[NSString stringWithFormat:@"%@",[_listItems objectAtIndex:0]];
    _label.font =[UIFont fontWithName:@"Arial Rounded MT Bold" size:20];
    _label.layer.cornerRadius =10;
    [self.view addSubview:_label];
    
    self.view .backgroundColor =[UIColor whiteColor];
    for(int i=1;i<[_listItems count];i++)
    {
        _button1 =[UIButton buttonWithType:UIButtonTypeCustom];
        _button1.frame=CGRectMake(10, 190+52*i, 300, 35);
        _button1.backgroundColor =[UIColor colorWithRed:0.05f green:0.8f blue:0.05f alpha:1];
        _button1.layer.cornerRadius =10;
        [_button1 setTitle:[NSString stringWithFormat:@"%@",[_listItems objectAtIndex:i]] forState:UIControlStateNormal];
        //        if(_label1.currentTitle)
        _button1.userInteractionEnabled =YES;
        [_button1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button1];
        UIImageView *image =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"_about_tele@2x.png"]];
        image.frame =CGRectMake(30, 3, 30, 25);
        [_button1 addSubview:image];
    }
    
    UIImageView* image4=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"call_living_p@2x.jpg"]];
    image4.frame =CGRectMake(10,94 ,90, 90);
    [self.view addSubview:image4];
}
#pragma mark--电话点击
-(void)btnClick:(id)sender
{
    [self htp:(sender)];
    users =[NSUserDefaults standardUserDefaults];
    NSArray *array =[users objectForKey:@"newusers1"];
    NSDictionary *dict =[array objectAtIndex:0];
    DSHisViewController *call =[[DSHisViewController alloc]init];
    [_listItems exchangeObjectAtIndex:0 withObjectAtIndex:1];
    if([dict objectForKey:@"username"]==nil)
    {
        UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"亲!还没绑定账号呢" message:nil delegate:self cancelButtonTitle:@"登录" otherButtonTitles:nil];
        [alertView show];
        return;
    }
    else
    {
        UIButton *resultButton = (UIButton *)sender;
        NSArray *hhah=[NSArray arrayWithObjects:resultButton.currentTitle,_label.text, nil];
        call.tellName=hhah;
        
        [self.navigationController pushViewController:call animated:YES];
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view removeFromSuperview];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
